package com.mpaster.meego.controller;

import com.mpaster.meego.entity.User;
import com.mpaster.meego.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@CrossOrigin
public class UserController {
    @Autowired
    private UserService userService;

    @GetMapping(value = "/users")
    public List<User> getUsers(){
        return userService.findAll();
    }

    @GetMapping("/users/{id}")
    public User getUserById(@PathVariable String id){
        return userService.findById(Long.valueOf(id));
    }

    @PostMapping("/users")
    public User addUser(User user){
        return userService.save(user);
    }

    @PutMapping("/users/{id}")
    public User updateUser(User user){
        return userService.save(user);
    }

    @DeleteMapping("/users/{id}")
    public void deleteUser(@PathVariable String id){
        userService.deleteUser(id);
    }

    @RequestMapping("/{lastName}")
    public List<User> getUser(@PathVariable String lastName){
        return userService.findByLastName(lastName);
    }






}
